Hi! Let's get to know each other. What is your name?
Hi! Let's get to know each other. What is your name?
"Ryan"
Then click "Save & Submit Code"Very good!
What would the length of your name be?
To discover the length of your name write your name within quotes. Then write a period (full stop) and the word length like this:"yourName".length
When you're done click Save & Submit Code
(Do this from now on every time you finish your exercise.).
In my case (my name is Leng) it would be"Leng".length
Great job! Now, let's do some math. You can do math through programming!
Add any two numbers, like this: 3 + 4
See what happened? You can use the command line to do basic math operations. Try playing around some more.
*
for multiplication and /
for division if you want. Enter another valid expression to pass this lessonThere are some things you can't do in the console. Computers only speak certain languages, like the one you've been using today: JavaScript!
If you use words that aren't in the JavaScript language, it will get confused and give you an error.
eggplant
. It will give you a ReferenceError.So far we've been writing lines of code in the editor. Now we see two lines that start with //
.
The //
sign is for comments. A comment is a line of text that JavaScript won't try to run as code. It's just for humans to read.
Comments make your program easier to understand. When you look back at your code or others want to collaborate with you, they can read your comments and easily figure out what your code does.
The computer will ignore the code on lines 1-2, since it is commented out.
On line 3, find the length of the word "cake".
This is JavaScript (JS), a programming language. There are many languages, but JS has many uses and is easy to learn.
What can we use JavaScript for?
What we just saw was a fun example of how JavaScript can be interactive. Try it yourself!
Examples:confirm("I feel awesome!");
confirm("I am ready to go.");
These boxes can be used on websites toconfirm things with users. You've probably seen them pop up when you try to delete important things or leave a website with unsaved changes.
Programming is like writing a list of instructions to the computer so it can do cool stuff with your information.
Programs can't yet make your bed, but they can do math, keep track of your bank account, or send a message to a friend.
To do any of these actions, the program needs an input. You can ask for input with aprompt.
Examples:
1. prompt("What is your name?");
2. prompt("What is Ubuntu?");
prompt
command to ask the user where they are from. Check out the examples above for how to do this!Data comes in various types. You have used two already!
a. numbers are quantities, just like you're used to. You can do math with them.
b. strings are sequences of characters, like the letters a-z
, spaces, and even numbers. These are all strings: "Ryan"
, "4"
and"What is your name?"
Strings are extremely useful as labels, names, and content for your programs.
To make a number in your code, just write a number as numerals without quotes: 42
,190.12334
.
To write a string, surround words with quotes: "What is your name?"
Find the length of the string by writing a period (full stop) and the word length
, like this:
Nice job! Next let's look at booleans. A boolean is either true
or false
.
For example, comparing two numbers returns a true
or false
result:
23 > 10
is true
5 < 4
is false
Let's compare two numbers to return a true result:
"I'm coding like a champ"
.length
If you want to check your code, click "Stuck? Get a hint!" below.
You may have noticed that the interpreter doesn't print out every single thing it does. So if we want to know what it's thinking, we sometimes have to ask it to speak to us.
console.log()
will take whatever is inside the parentheses and log it to the consolebelow your code—that's why it's calledconsole.log()
!
This is commonly called printing out.
Please print the following two console.log statements at the same time. Type one online 1 and the other on line 2. Then press Save & Submit Code.
console.log(2 * 5)
console.log("Hello")
So far we've learned about three data types:
"dogs go woof!"
)4
, 10
)false
, 5 > 4
)Now let's learn more about comparison operators.
List of comparison operators:
>
Greater than<
Less than<=
Less than or equal to>=
Greater than or equal to===
Equal to!==
Not equal toTry to use each of the operators above.
true
.Nice work on comparisons! Now let's see how we can use comparisons to ask yes or no questions.
Say we want to write a program that asks whether your name is longer than 7 letters. If the answer is yes, we can respond with "You have a long name!" We can do this with an if
statement:
if
statement is made up of the if
keyword, a condition like we've seen before, and a pair of curly braces { }
. If the answer to the condition is yes, the code inside the curly braces will run.Check out the if
statement in the editor.
Great! We used an if
statement to do something if the answer to the condition was yes, or true
as we say in JavaScript.
In addition to doing something when the condition is true
, we can do something else if the condition is false
. For example, if your name is shorter than 7 letters, we can respond with "You have a short name!" We can do this using an if
/ else
statement:
Just like before, if the condition is true
, then only the code inside the first pair of curly braces will run. Otherwise, the condition is false
, so only the code inside the second pair of curly braces after theelse
keyword will run.
In the example above the condition"myName".length >= 7
evaluates to false
since "myName"
only has 6 letters. Since the condition is false
, only the code inside the curly braces after the else
keyword runs, and prints out You have a short name!
.
10 < 5
:false
else
portion (this will run if the condition isfalse
). Use console.log
for this part.Now let's practice using if
/else
statements. Do as much as you can by yourself, but if you need a reminder, click the "Stuck? Get a hint!" button below.
if
/else
statement, just like we did in the last exercise. Here's what the outline of the code looked like:If your condition is true
, use console.log
to print "The condition is true"
.
Otherwise (else) when it is false
, useconsole.log
to print "The condition is false"
.
Make sure your condition evaluates tofalse
, so that your program prints out"The condition is false"
.
Well done! Now, computers are very literal. Syntax needs to be in exactly the right place for the computer to understand the code.
As you get started with programming, we will teach you many syntax rules. This is sort of like the grammar of programming languages. Grammar first, then programming poetry!
There are many mistakes in this code. Find them and fix them all.
You are doing what's called "debugging," a term popularized by Grace Hopper when she literally removed a moth from her computer.
We've covered a lot of ground so far! So many new terms, so much syntax. Let's take a breather and review. We have learned:
1. Confirm and prompt
We can make pop-up boxes appear! confirm("I am ok");
prompt("Are you ok?");
2. Data types
a. numbers (e.g. 4.3
, 134
)
b. strings (e.g. "dogs go woof!"
,"JavaScript expert"
)
c. booleans (e.g. false
, 5 > 4
)
3. Conditionals
If the first condition is met, execute the first code block. If it is not met, execute the code in the else
block. See the code on the right for another example.
Hope this breather was helpful! Click 'Save and Submit' to continue.
We saw basic math before. The basic math symbols we learned in school work here. Even the order in which the computer understands the math is the same as in school!
Code:
1. ( )
: control order of operations
2. *
and /
: multiplication and division
3. -
and +
: subtraction and addition
Examples:
1. 100/10
evaluates to 10
2. "Jane".length + 5
evaluates to 9
3. 5*(3+1)
evaluates to 20
Let's meet an interesting symbol calledmodulo. When %
is placed between two numbers, the computer will divide the first number by the second, and then return theremainder of that division.
So if we do 23 % 10
, we divide 23 by 10 which equals 2 with 3 left over. So 23 % 10
evaluates to 3
.
More examples:17 % 5
evaluates to 213 % 7
evaluates to 6
Use console.log and modulo three times to print the remainder of the following equations:
a. 14 / 3
b. 99 / 8
c. 11 / 3
So why learn modulo? For one thing, it's good at testing divisibility. Consider 30 % 10
. What does it return? There is nothing left over, so 0
.
How about 9 % 3
? Also 0
.
We can use modulos in comparisons, like this:
10 % 2 === 0
evaluates to true
7 % 3 === 0
evaluates to false
because there is 1 left over.
Let's get the if
/else
" statement to display "The first number is even"
.
true
.We've learned a few ways to manipulate numbers. What about manipulating strings?
Sometimes you don't want to display theentire string, just a part of it. For example, in your Gmail inbox, you can set it to display the first 50 or so characters of each message so you can preview them. This preview is a substring of the original string (the entire message).
Code:"some word".substring(x, y)
where x
is where you start chopping and y
is where you finish chopping the original string.
The number part is a little strange. To select for the "he" in "hello", you would write this:
The letter h
is in position 0
, the letter e
is in position 1
, and so on.
Therefore if you start at position 0
, and slice right up till position 2
, you are left with just he
More examples:
1. First 3 letters of "Batman""Batman".substring(0,3);
2. From 4th to 6th letter of "laptop""laptop".substring(3,6);
"wonderful day"
.Getting the positioning of substring letter positions is tricky! Let's make sure we really have it nailed down.
Remember that each character in a string is numbered starting from 0. So for the word "hello", The letter h
is in position 0
, the letter e
is in position 1
, and so on.
Using console.log
, on three separate lines, print out the substrings for the following strings.
a. "Jan" in "January"
b. "Melbourne is" in "Melbourne is great" (note the space!)
c. "burgers" in "Hamburgers"
We have learned how to do a few things now: make strings, find the length of strings, find what character is in the nth position, do basic math. Not bad for a day's work!
To do more complex coding, we need a way to 'save' the values from our coding. We do this by defining a variable with a specific, case-sensitive name. Once you create (ordeclare) a variable as having a particular name, you can then call up that value by typing the variable name.
Code:var varName = data;
Example:
a. var myName = "Leng";
b. var myAge = 30;
c. var isOdd = true;
myAge
and type in your age.We have seen how to create a variable. But how do we use it? It is useful to think that any time you type the variable's name, you are asking the computer to swap out the variable name and swap in the value of the variable.
For example:var myName = "Steve Jobs";
myName.substring(0,5)
Look at the second line above. You have asked the computer to swap out myName
and swap in Steve Jobs
, so
myName.substring(0,5)
becomes
"Steve Jobs".substring(0,5)
which evaluates to Steve
.
Another example
var myAge = 120;
What is
myAge % 12
? See the hint to check your answer.
So the variable stores the value of the variable, whether that is a number or a string. As you will see soon, this makes writing long programs much easier!
So far, we've seen
a. how to create a variable
b. how to use a variable
Let's now see how to change a variable's value. A variable's value is easily changed. Just pretend you are creating a new variable while using the same name of the existing variable!
Example:var myAge = "Thirty";
Say I had a birthday and I want to change my age.myAge = "Thirty-one";
Now the value of myAge
is "Thirty-one"!
Let's do a quick review!
Data types
"dogs go woof!"
)4
, 10
)false
, 5 > 4
)Variables
We store data values in variables. We can bring back the values of these variables by typing the variable name.
Manipulating numbers & strings
>
, <=
)%
)"Emily".length;
)"hi".substring(0, 1);
)console.log( )
Prints into the console whatever we put in the parentheses.
Congratulations on making it this far. You have learned a lot! Just one more exercise before a big pat on the back!
The last tricky thing we learned was aboutif / else statements.
If / else statements are conditional statements. Under different conditions, the computer will output different things.